package utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonUtil {

	private Map<String, String> rootMap;
	private JSONArray map;
	private String fullJobState;

	public JsonUtil() {
		try {
			this.rootMap = new HashMap<>();
			this.map = new JSONArray();
			StringBuilder jobState = new StringBuilder();
			String jobStatePath = this.getClass().getResource("/").getPath().replaceAll("%20", " ");
			BufferedReader stateBr = new BufferedReader(
					new FileReader(new File(jobStatePath+"/fullStateTest.json")));
			String tempStateString = null;
			while ((tempStateString = stateBr.readLine()) != null) {
				jobState.append(tempStateString);
			}
			fullJobState = jobState.toString();
			StringBuilder gg = new StringBuilder();
			String path = this.getClass().getResource("/").getPath().replaceAll("%20", " ");
			BufferedReader br = new BufferedReader(
					new FileReader(new File(path+"/fileJsonFinal.json")));
			String tempString = null;
			while ((tempString = br.readLine()) != null) {
				gg.append(tempString);
			}
			br.close();
			JSONArray jsonArray = JSONArray.fromObject(gg.toString());
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject one = new JSONObject();
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				one.put("pathSuffix", jsonObject.getString("pathSuffix"));
				one.put("type", jsonObject.getString("type"));
				one.put("childrenNum", jsonObject.getJSONArray("children").size());
				this.map.add(one);
			}
			JSONArray tempArray = new JSONArray();
			while (true) {
				for (int i = 0; i < jsonArray.size(); i++) {
					putFileInfor(jsonArray.getJSONObject(i));
					JSONArray childrenArray = jsonArray.getJSONObject(i).getJSONArray("children");
					for (int mm = 0; mm < childrenArray.size(); mm++) {
						tempArray.add(childrenArray.getJSONObject(mm));
					}
				}
				if (tempArray.size() == 0)
					break;
				jsonArray = tempArray;
				tempArray = new JSONArray();
			}
		} catch (IOException e) {
			e.printStackTrace();
			rootMap = null;
		}
	}

	private void putFileInfor(JSONObject jsonObject) {
		JSONArray childrens = jsonObject.getJSONArray("children");
		JSONArray array = new JSONArray();
		for(int i=0;i<childrens.size();i++){
			JSONObject one = new JSONObject();
			JSONObject oneObject = childrens.getJSONObject(i);
			one.put("pathSuffix", oneObject.getString("pathSuffix"));
			one.put("type", oneObject.getString("type"));
			one.put("childrenNum", oneObject.getJSONArray("children").size());
			array.add(one);
		}
		if(jsonObject.getString("pathSuffix").equals("a_b")){
			JSONArray jsonArray = new JSONArray();
			for(int i=0;i<3005;i++){
				JSONObject jsonObject1 = new JSONObject();
				jsonObject1.put("pathSuffix",i+".jpg");
				jsonObject1.put("type","FILE");
				jsonObject1.put("childrenNum",0);
				jsonArray.add(jsonObject1);
			}
			this.rootMap.put(jsonObject.getString("pathSuffix"), jsonArray.toString());
		}else {
			this.rootMap.put(jsonObject.getString("pathSuffix"), array.toString());
		}
	}

	/**
	 * get file path
	 * @param root (null:root path,directory name:directory path)
	 * @return
	 */
	public String getPathInforByRoot(String root) {
		if (root == null||root.equals("")) {
			return map.toString();
		} else {
			return rootMap.get(root);
		}
	}

	public String getFullJobState(){
		return fullJobState;
	}

	public static void main(String[] args) {
		JsonUtil util = new JsonUtil();
	}
}
